//
//  WWHomeViewController.m
//  Shopping
//
//  Created by 张湾湾 on 16/8/18.
//  Copyright © 2016年 张湾湾. All rights reserved.
//

#import "WWHomeViewController.h"
#import "UIBarButtonItem+WWBarButton.h"
#import "SDCycleScrollView.h"
#import "WWHeaderViewModel.h"
#import "WWIconView.h"
#import "WWActivitiesView.h"
#import "WWNetworkTools.h"
#import "WWIconModel.h"
#import "WWHeaderView.h"
#import "WWIconViewController.h"
#import "WWActiveModel.h"
#import "WWFreshSellModel.h"
#import "QRCodeViewController.h"
@interface WWHomeViewController ()<SDCycleScrollViewDelegate>

@property (nonatomic,strong)WWHeaderView *headView;
/**图片轮播URL数组*/
@property (nonatomic,strong)NSArray<WWHeaderViewModel *>  *imagesURLStrings;
/**四个按钮的view*/
@property (nonatomic,strong)WWIconView * iconView;
/**活动*/
@property (nonatomic,strong)WWActivitiesView *activeView;

@end

@implementation WWHomeViewController

- (void)test{
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    //添加nav
    [self setupNav];

    [self setupUI];
    [self getData];
    
}

#pragma mark - 设置nav -
- (void)setupNav{
    self.navigationItem.leftBarButtonItem = [UIBarButtonItem barButtonItemWithTitle:@"扫一扫" image:@"icon_black_scancode" target:self action:@selector(didClickLeftItem)];
    self.navigationItem.rightBarButtonItem = [UIBarButtonItem barButtonItemWithTitle:@"搜索" image:@"icon_search" target:self action:@selector(didClickRightItem)];
    
}

- (void)didClickLeftItem{
    QRCodeViewController * qr = [QRCodeViewController new];
    UINavigationController * nav = [[UINavigationController alloc]initWithRootViewController:qr];
    [self.navigationController presentViewController:nav animated:YES completion:nil];
}
- (void)didClickRightItem{
    NSLog(@"点击了搜索");
}
#pragma mark - 布局子控件 -
- (void)setupUI{
    [self.headView makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.view);
        make.size.equalTo(CGSizeMake(SCREENW, 120));

    }];
    [self.iconView makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.headView.bottom);
        make.size.equalTo(CGSizeMake(SCREENW, 65));
        
    }];
    
    [self.activeView makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.iconView.bottom).offset(MARGINCELL);
        make.size.equalTo(CGSizeMake(SCREENW, 400));
    }];
    
 }
#pragma mark - 请求数据 -
- (void)getData{
    
    [[WWNetworkTools sharedTools] getHomeWithSuccess:^(id response) {
        
        NSDictionary * dic = response[@"data"];
        /**轮播图*/
        NSArray * focus = dic[@"focus"];
        NSMutableArray <WWHeaderViewModel*>*headerMutArray = [NSMutableArray array];
        for (NSDictionary *dic in focus) {
            WWHeaderViewModel * icon = [WWHeaderViewModel modelWithDictionary:dic];
            [headerMutArray addObject:icon];
        }
        /**按钮*/
        NSArray * icon = dic[@"icons"];
        NSMutableArray <WWIconModel*>* iconMutArray = [NSMutableArray array];
        for (NSDictionary * dic in icon) {
            WWIconModel * model = [WWIconModel modelWithDictionary:dic];
            [iconMutArray addObject:model];
        }
        
        /** 活动 */
        NSArray *activities = dic[@"activities"];

        NSMutableArray <WWActiveModel *> *activitiesMutArr = [NSMutableArray array];
        
        for (NSDictionary *dict in activities) {
            WWActiveModel *model  = [WWActiveModel modelWithDictionary:dict];
            
            [activitiesMutArr addObject:model];
        }

        self.headView.imageArray = headerMutArray.copy;
        self.iconView.iconModel = iconMutArray.copy;
        self.activeView.activities = activitiesMutArr.copy;
        
    } failure:^(NSError *error) {
        
    }];
    
    //跳转
    
    __weak typeof(self)weakSelf = self;
    self.iconView.returnblk = ^(NSString * urlString){
        
        
        WWIconViewController * vc = [WWIconViewController new];
        vc.urlString = urlString;
        
        [weakSelf.navigationController  pushViewController:vc animated:YES];

    };
   
    /**新鲜热卖*/
    [[WWNetworkTools sharedTools] fruitSellWithSuccess:^(NSString *response) {
        NSDictionary *dictionary = (NSDictionary *)response;
        NSArray *freshInfo = dictionary[@"data"];
        
        NSMutableArray<WWFreshSellModel *> *mArr = [NSMutableArray array];
        for (NSDictionary *dict in freshInfo) {
            WWFreshSellModel *model = [WWFreshSellModel modelWithDict:dict];
            [mArr addObject:model];
        }
        
        self.activeView.freshInfo = mArr.copy;
//        NSLog(@"%@",mArr.copy);
    } failure:^(NSError *error) {
        NSLog(@"获取新鲜热卖数据失败:%@",error);
    }];
}

#pragma mark - 懒加载 -
-(WWIconView*)iconView{
    if (!_iconView) {
        WWIconView * iconView = [[WWIconView alloc]init];
        _iconView = iconView;
        [self.view addSubview:iconView];
    }
    return _iconView;
}
- (WWActivitiesView *)activeView{
    if (!_activeView) {
        WWActivitiesView * activeView = [[WWActivitiesView alloc]init];
        _activeView = activeView;
        [self.view addSubview:activeView];
    }
    return _activeView;
}
- (WWHeaderView *)headView{
    if (!_headView) {
        WWHeaderView * headView = [WWHeaderView new];
        [self.view addSubview:headView];
        _headView = headView;
    }
    return _headView;
}
@end
